import Vue from 'vue'
// 安装vuex
import Vuex from 'vuex'

Vue.use(Vuex)

import {
    CHANGE_TITLE,
    SET_MUSIC_INFO,
    CHANGE_CURRENTTIME,
    CHANGE_PROCESS
} from '../constant/vuex-mutation'

import actions from './action'

import moduleA from './modules/moduleA'

// store是管理员，通过store可以获取数据，也可以修改数据
const store = new Vuex.Store({
    state: {
        count: 0,
        title: 'vuex中的title',
        obj: {
            title: 123,
            name: '张三'
        },
        // 歌曲详情信息
        songInfo: {},
        bitrate: {},
        // 进度
        currentTime: 0,
        process: 0
    },
    getters: {
        title2: state => {
            return state.title + '2';
        }
    },
    mutations: {
        increment(state, count) {
            state.count = count
        },
        [CHANGE_TITLE](state, payload) {
            state.title = payload.title;
            // 不能这么写的
            state.obj = {
                ...state.obj,
                title: payload.title
            }
        },
        [SET_MUSIC_INFO](state, payload) {
            state.songInfo = payload.songInfo;
            state.bitrate = payload.bitrate
        },
        // 用于修改进度
        [CHANGE_CURRENTTIME](state, payload) {
            state.currentTime = payload.currentTime
        },
        [CHANGE_PROCESS](state, payload) {
            // 修改state
            state.process = payload.process
        }
    },
    actions,
    modules: {
        a: moduleA
    }
})

console.log(store.state.count);
store.commit('increment', 15);
console.log(store.state.count);

export default store